//
//  Toast+HUD.swift
//  Social
//
//  Created by 李响 on 2019/5/30.
//  Copyright © 2019 shengsheng. All rights reserved.
//

import Foundation
import SwiftMessages

extension Toast {
    
    private static let messages = SwiftMessages()
    private static var workItem: DispatchWorkItem?
    
    /// 显示消息
    ///
    /// - Parameter msg: 消息内容
    static func show(hud msg: String, in presentation: Presentation = .automatic) {
        _show(msg, in: presentation)
    }
}

extension Toast {
    /// 显示层级
    enum Presentation {
        case automatic
        case window(UIWindow.Level)
        case viewController(UIViewController)
        case view(UIView)
        
        var context: SwiftMessages.PresentationContext {
            switch self {
            case .automatic: return .automatic
            case let .window(value): return .window(windowLevel: value)
            case let .viewController(value): return .viewController(value)
            case let .view(value): return .view(value)
            }
        }
    }
}

extension Toast {
    
    private static func _show(_ msg: String, in presentation: Presentation = .automatic) {
        guard !msg.isEmpty else { return }
        
        if let current: HUDView = messages.current() {
            current.set(content: msg)
            
        } else {
            let view = HUDView()
            view.set(content: msg)
            var config = SwiftMessages.defaultConfig
            config.shouldAutorotate = false
            config.interactiveHide = false
            config.duration = .forever
            config.presentationContext = presentation.context
            config.presentationStyle = .center
            messages.show(config: config, view: view)
        }
        
        workItem?.cancel()
        let item = DispatchWorkItem {
            messages.hideAll()
        }
        workItem = item
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: item)
    }
    
    fileprivate static func hide() {
        workItem?.cancel()
        messages.hideAll()
    }
}

fileprivate class HUDView: UIView {
    
    private lazy var view: UIView = {
        $0.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6)
        $0.cornerRadius = 10
        return $0
    } (UIView())
    
    private lazy var label: UILabel = {
        $0.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        $0.numberOfLines = 0
        $0.textAlignment = .center
        $0.font = .systemFont(ofSize: 15, weight: .light)
        return $0
    } (UILabel())
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
        setupLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
        setupLayout()
    }
    
    private func setup() {
        addSubview(view)
        addSubview(label)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        view.addGestureRecognizer(tap)
    }
    
    private func setupLayout() {
        view.snp.makeConstraints { (make) in
            make.top.bottom.centerX.equalToSuperview()
        }
        label.snp.makeConstraints { (make) in
            make.width.lessThanOrEqualTo(280)
            make.height.lessThanOrEqualTo(screenHeight * 0.5)
            make.edges.equalTo(view).inset(UIEdgeInsets(horizontal: 60, vertical: 40))
        }
    }
    
    @objc
    private func tapAction(_ sender: UITapGestureRecognizer) {
        Toast.hide()
    }
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return convert(view.frame, from: self).contains(point)
    }
}

extension HUDView {
    
    func set(content: String) {
        label.text = content
    }
}
